package logmanager.view_connection;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JPanel;

import logmanager.proxy.ServerLogManager;
import logmanager.view.ActionQuit;
import logmanager.view.Principal;

//classe d'affichage de la page de connection
public class ConnectionView extends JPanel{
	private static final long serialVersionUID = 5706449322857281108L;
	
	public JButton buttonConnection, buttonQuit;
	public ServerLogManager logManager;
	public Principal p;
	public String propertiesFile = "properties.txt";
	private String ws_host, ws_port;
	
	public ConnectionView(Principal p, ServerLogManager logManager) {
		this.logManager = logManager;
		this.p = p;
		initConnectionView();
	}
	
//initialisation de la page
	public void initConnectionView(){
				
		this.setLayout(new BorderLayout());
		JPanel pNorth, psouth, pEst, pWest, pCenter;
		pNorth = new JPanel(); psouth = new JPanel(); pEst = new JPanel(); pWest = new JPanel(); pCenter = new JPanel();
		pNorth.setPreferredSize(new Dimension(0, 180)); pNorth.setOpaque(false);
		psouth.setPreferredSize(new Dimension(0, 220)); psouth.setOpaque(false);
		pEst.setPreferredSize(new Dimension(350, 0)); pEst.setOpaque(false);
		pWest.setPreferredSize(new Dimension(350, 0)); pWest.setOpaque(false);
		pCenter.setOpaque(false);
		
		
		this.add("North", pNorth);this.add("South", psouth); this.add("East", pEst); this.add("West", pWest);
		
		Form f = new Form("Parametre de connexion", 60, Form.VERTICAL);
		f.addField("Serveur", 15);
		f.addField("Port", 5);
		pCenter.add(f);
		
		buttonConnection = new JButton("Connection");
		buttonQuit = new JButton("Quitter");
		JPanel pa = new JPanel();
		pa.add(buttonConnection);
		pa.add(buttonQuit);
		
		pCenter.add(pa);
		
		this.add("Center", pCenter);
		
		ArrayList<JPanel> panes = new ArrayList<JPanel>();
		panes.add(this);
		panes.add(f);
		panes.add(pa);
		
		DarkenPane darken = new DarkenPane(panes);
		darken.start();

		buttonQuit.addMouseListener(new ActionQuit());
		buttonConnection.addMouseListener(new ActionConnection(this.p, logManager, f.getTextField(0), f.getTextField(1)));
		
		if(loadProperties() == 0){
			f.getTextField(0).setText(ws_host);
			f.getTextField(1).setText(ws_port);
			
		//	f1.getTextField(0).setText(rmi_host);
		//	f1.getTextField(1).setText(rmi_service);
		//	f1.getTextField(2).setText(rmi_port);
		}
	}

//chargement des proprietes depuis le fichier de confic
	public int loadProperties(){
		try{
			Properties prop = new Properties();
			FileInputStream fin = new FileInputStream("setting/conf.properties");
			prop.load(fin);

			ws_host = prop.getProperty("ws_host");
			ws_port = prop.getProperty("ws_port");
			fin.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return 0;
	}
}
